<?php
$xtpl = new XTemplate("modules/home/default_image.htm");
//--
//--
//--
//--

if($_POST[type] == 'get_back'){
	DelImgAfterGetBack('top_menu_bg_img_1','0');
	DelImgAfterGetBack('top_menu_bg_img_2','0');
	DelImgAfterGetBack('top_menu_bg_img_3','0');
	DelImgAfterGetBack('btn_menu_bg_img','0');
	DelImgAfterGetBack('common_btn_bg_img','0');
	DelImgAfterGetBack('box_title_img','0');
	DelImgAfterGetBack('box_top_img','0');
	DelImgAfterGetBack('box_bottom_img','0');
	//--
	mysql_query("DELETE FROM ofst_site_images WHERE member_id = '0' AND is_stored_images = '0'");
	//--
	$sql = "
			INSERT INTO {$DP}site_images (member_id,si_name,si_image)
			SELECT member_id,si_name,si_image FROM {$DP}site_images WHERE member_id = '0' AND is_stored_images = '1'
		   ";
	mysql_query($sql);	
	//-- Redir...
	$GUI->redir("?mod=home&catepath=".$_GET[catepath]."&act=default_image");
}
//--
if($_POST[type] == 'update'){
	//--------
	// If new images uploaded. Delete the old images. Upload the new images,
	//    then update the database with new records.
	//
	// If the old records are backup-datas from default site images. Dont delete the images on the host, 
	//    just upload the file then update the database.
	//--------
	
	if($_FILES[top_menu_bg_img_1]['name'] != ''){
		UpdateIMAGE('top_menu_bg_img_1',$_FILES[top_menu_bg_img_1]['type'],$_FILES[top_menu_bg_img_1]['name'],'0','1004');
	}
	//--
	if($_FILES[top_menu_bg_img_2]['name'] != ''){
		UpdateIMAGE('top_menu_bg_img_2',$_FILES[top_menu_bg_img_2]['type'],$_FILES[top_menu_bg_img_2]['name'],'0','1004');
	}
	//--
	if($_FILES[top_menu_bg_img_3]['name'] != ''){
		UpdateIMAGE('top_menu_bg_img_3',$_FILES[top_menu_bg_img_3]['type'],$_FILES[top_menu_bg_img_3]['name'],'0','1004');
	}
	//--
	if($_FILES[btn_menu_bg_img]['name'] != ''){
		UpdateIMAGE('btn_menu_bg_img',$_FILES[btn_menu_bg_img]['type'],$_FILES[btn_menu_bg_img]['name'],'0','143');
	}
	//--
	if($_FILES[common_btn_bg_img]['name'] != ''){
		UpdateIMAGE('common_btn_bg_img',$_FILES[common_btn_bg_img]['type'],$_FILES[common_btn_bg_img]['name'],'0','51');
	}
	//--
	if($_FILES[box_title_img]['name'] != ''){
		UpdateIMAGE('box_title_img',$_FILES[box_title_img]['type'],$_FILES[box_title_img]['name'],'0','207');
	}
	//--
	if($_FILES[box_top_img]['name'] != ''){
		UpdateIMAGE('box_top_img',$_FILES[box_top_img]['type'],$_FILES[box_top_img]['name'],'0','207');
	}
	//--
	if($_FILES[box_bottom_img]['name'] != ''){
		UpdateIMAGE('box_bottom_img',$_FILES[box_bottom_img]['type'],$_FILES[box_bottom_img]['name'],'0','207');
	}
	//--
}
//--
function UpdateIMAGE($file,$file_type_,$file_name_,$member_id_u,$resize_to){
	global $DP,$DB,$GUI,$sitePath,$rootPath;
	//--
	$allow_extention = array('gif'=>1, 'jpg'=>1, 'jpeg'=>1, 'png'=>1);
	$filetype = substr($file_type_,6,strlen($file_type_)-6);
	//--
	if($allow_extention[$filetype] != 1){
		echo 'dont allow';
	}elseif($allow_extention[$filetype] == 1){
		//--------
		$path = "images/";
		$uploaded_img = upload_to_server($path,$file);
		//-- If image uploaded:
		if(isset($uploaded_img)){
			$key = 1;
			//--
			DelImgAfterGetBack($file,$member_id_u);
			//--
			$si_image_insert = $file.$uploaded_img;
			//-- Query
			mysql_query("UPDATE {$DP}site_images SET si_image='".$si_image_insert."' WHERE si_name = '".$file."' AND member_id = '".$member_id_u."' AND is_stored_images = '0'");
		}
		////////RESIZE//////////	
		if($key == 1){
			$imgfile = $rootPath."/images/".$uploaded_img;
			@$img_size = getimagesize($imgfile);
			$width = $img_size[0];
			if($width > $resize_to){
				$thumbsize = $resize_to;
			}else{
				$thumbsize = $width;
			}
			$thumbpart = $rootPath."/images/".$file.$uploaded_img;
			resizeimage($imgfile, $thumbpart, $thumbsize, $filetype);	
			//-- Delete large image
			$del_large_img = $rootPath."/images/".$uploaded_img;
			@unlink($del_large_img);		
		}		
	}
}
function DelImgAfterGetBack($file,$member_id_u){
	global $DP,$DB,$GUI,$sitePath,$rootPath;
	
	$row__1 = $DB->SQLrecordset("SELECT si_image FROM {$DP}site_images WHERE si_name = '".$file."' AND member_id = '0' AND is_stored_images = '1'");
	$row__2 = $DB->SQLrecordset("SELECT si_image FROM {$DP}site_images WHERE si_name = '".$file."' AND member_id = '".$member_id_u."' AND is_stored_images = '0'");
	//-- If not equal, delete the old file.
	if($row__1[si_image] != $row__2[si_image]){
		$del_img = $rootPath."/images/".$row__2[si_image];
		@unlink($del_img);
	}
}
//--
//--
//--
//--
//--
//--
$waypathlink = '&raquo;&nbsp;<a href="?mod=home&catepath='.$_GET[catepath].'&act=default_image" class="waypath">Default Images</a>';
$xtpl_main->assign("waypathlink",$waypathlink);
//--
//--
//--
//--
//--
//--
$save_button = $GUI->button_ren("Save","javascript: doUpdateDefaultImage('update');");
$xtpl->assign("save_button",$save_button);
$get_back_button = $GUI->button_ren("Get Back","javascript: doGetBackDefaultImage('get_back');");
$xtpl->assign("get_back_button",$get_back_button);
?>